

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layuiAdmin 内容系统 - 文章列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
  <link rel="stylesheet" href="../../layuiadmin/layui_ext/dtree/dtree.css" media="all">
  <link rel="stylesheet" href="../../layuiadmin/layui_ext/dtree/font/dtreefont.css" media="all">
  <style>
    body .layui-body{
        left: 310px;
        top: 0px
    }
  </style>
</head>
<body>
  <div class="layui-fluid" style="display: inline-block; width: 307px; border: 1px solid #ddd; padding: 0;overflow: hidden;">
    <div class="layui-card">
      <div class="layui-card-body">
        <div style="padding-bottom: 10px; position: relative;">
          <button class="layui-btn layuiadmin-btn-list equipment-add-button" data-type="addCategories"><i class="layui-icon layui-icon-add-1"></i>添加专业分类</button>
          <ul id="menubarTree" class="dtree" data-id="0"></ul>
        </div>
      </div>
    </div>
  </div>
  <div class="layui-body" id="LAY_app_body">
    <div class="layadmin-tabsbody-item layui-show">
      <iframe src="equipment_classification.html" frameborder="0" class="layadmin-iframe" name="tree" id="iframe_content" ></iframe>
    </div>
</div>
<script src='../../layuiadmin/modules/jTools.js'></script>
<script src="../../layuiadmin/layui/layui.js"></script>  
<script>
  layui.config({
    base: '../../layuiadmin/layui_ext/dtree/' //配置 layui 第三方扩展组件存放的基础目录
  }).extend({
    dtree: 'dtree' //定义该组件模块名
    , my_send: '../myModules/my_send'
  }).use(['dtree','layer','jquery'], function(){
    var dtree = layui.dtree, layer = layui.layer, $ = layui.jquery;
  my_send = layui.my_send
            , http_proxy = layui.http_proxy;
    var nodes = '';
    var DTree5 =  dtree.render({
      elem: "#menubarTree",
      url:http_proxy('/materialCaregory/getFMcTree'),
      initLevel: 1,
      line: false, // 无树线
      ficon: ["2","-1"],  // 设定一级图标样式。1表示三角形图标，-1表示不设置叶子节点图标
      icon: "-1", // 不设定二级图标样式。
      response:{
        message: "message", //返回信息（必填）
        statusCode: 200, //返回码（必填）
        statusName: "code", //返回标识（必填）
        rootName: "rows",
        treeId: "fMcId", //节点ID（必填）
        parentId:'fParent',
        title: "fMcName", //节点名称（必填）
        childName:"children",
        level:'fLevel',
        basicData: "basicData.basicDataList", //用户自定义的记录节点数据
      },
      toolbar:true,
      scroll:"#toolbarDiv5",
      toolbarShow:[], // 默认按钮制空
      // toolbarFun:{
      //   loadToolbarBefore: function(buttons,treeNode){ // 右键菜单加载前的函数
      //       if(treeNode.level ==1 || treeNode.level ==2){
      //           return buttons;
      //       }else {
      //         return  true;
      //       }
      //   },
      // },
     
      toolbarExt:[{toolbarId: "testAdd",icon:"dtree-icon-wefill",title:"增加下级分类",handler: function(node,$div){
        layer.msg(JSON.stringify(node));
        // 你可以在此添加一个layer.open，里面天上你需要添加的表单元素，就跟你写新增页面是一样的
        layer.open({
          type: 2
          ,title: '增加下级分类'
          ,content: 'addForm.html'
          ,maxmin: true
          ,area: ['550px', '450px']
          ,btn: ['确定', '取消']
          ,success: function(layero, index){
            let iframe = window['layui-layer-iframe'+index];
          }
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
            ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
            //监听提交
            iframeWindow.layui.form.render();
            iframeWindow.layui.form.on('submit(layuiadmin-app-form-submit)', function(data){
              var field = data.field;
              field.fHasChild = field.fHasChild === '是' ? "1":"0";
              field.fStatus = 1;
              var json = {"fMcId":field.fMcId,"fMcName": field.fMcName,"parentId":node.nodeId,"level":node.level};
              field.fParent = json.parentId;
              field.fLevel = json.level;
              $.ajax({
                url:http_proxy('/materialCaregory/saveMc'),
                type:'post',
                data:field,
                success:function(data){
                  if(data.code = "success"){ 
                    // initTree();
                    DTree5.partialRefreshAdd($div, json); // 如果是json对象，则会追加元素
                    debugger
                    layer.msg(data.message,{icon:6});
                  }else{
                    layer.msg(data.message,{icon:5});
                  }
                }
              })
              layer.close(index);
                return false;
            });  
            submit.trigger('click');
          }
        }); 
      }
    },
    {toolbarId: "testEdit",icon:"dtree-icon-bianji",title:"编辑分类",handler: function(node,$div){
        layer.msg(JSON.stringify(node.nodeId));
        // 你可以在此添加一个layer.open，里面天上你需要添加的表单元素，就跟你写编辑页面是一样的
        debugger
        layer.open({
          type: 2
          ,title: '编辑分类信息'
          ,content: 'editForm.html?fMcId=' + node.nodeId
          ,maxmin: true
          ,area: ['550px', '450px']
          ,btn: ['确定', '取消']
          ,success: function(layero, index){
            let iframe = window['layui-layer-iframe'+index];
            iframe.initNodeData(node);
          }
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
            ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
            //监听提交
            iframeWindow.layui.form.render();
            iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function(data){
              var field = data.field;
              field.fHasChild = field.fHasChild === '是' ? "1":"0";
              field.fStatus = 1;
              var json = {"fMcId":node.nodeId,"fMcName": field.fMcName,"parentId":node.parentId,"level":node.level};
              field.fParent = json.parentId;
              field.fMcId = json.fMcId;
              debugger
              field.fLevel = json.level;
              $.ajax({
                url:http_proxy('/materialCaregory/saveMc'),
                type:'post',
                data:field,
                success:function(data){
                  if(data.code = "success"){ 
                    // initTree();
                    DTree5.partialRefreshEdit($div, json); // 如果是json对象，则会追加元素
                    debugger;
                    layer.msg(data.message,{icon:6});
                  }else{
                    layer.msg(data.message,{icon:5});
                  }
                }
              })
              layer.close(index);
                return false;
            });  
            submit.trigger('click');
          }
        })
      }
    },
    {toolbarId: "testDel",icon:"dtree-icon-roundclose",title:"删除分类",handler: function(node,$div){
      // layer.msg(JSON.stringify(node));
      $.ajax({
        url:http_proxy('/materialCaregory/deleteMc'),
        type:'post',
        data:{
          fMcId:node.nodeId
        },
        success:function(data){
          if(data.code = 'success'){
            DTree5.partialRefreshDel($div); // 这样即可删除节点
            debugger
            layer.msg(data.message,{icon:6});
          }else{
            layer.msg(data.message, {icon: 5});
          }
        }
      })
    },
    }]
    });
    dtree.on("node('menubarTree')", function(data){
      $('#iframe_content').attr('src','equipment_classification.html?fMcId='+ data.param.nodeId )
      layer.msg(JSON.stringify(data.param));
      // layer.msg(JSON.stringify(data.param.fMcId));
    });
    var $ = layui.$, active = {
      addCategories: function(){
        layer.open({
          type: 2
          ,title: '添加专业分类'
          ,content: 'equipment_classifyform.html'
          ,maxmin: true
          ,area: ['550px', '450px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            //点击确认触发 iframe 内容中的按钮提交
            var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
            submit.click();
          },
          end:function(){
            dtree.reload("menubarTree",{
              url: http_proxy("/materialCaregory/getFMcTree"),
              initLevel: "1",
            });
          }
        }); 
      }
    }; 
    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
  </script>
</body>
</html>
